﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class web_pages_dangkynhatuyendung_nextstep : System.Web.UI.Page
{
    public string mess = "";
    public objDoanhNghiep objNTD;
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Title = "Các bước đăng ký nhà tuyển dụng " + ConfigurationManager.AppSettings["centername"];
        if (Session["NTDobj"] != null)
        {
            objNTD = (objDoanhNghiep)Session["NTDobj"];
        }
        else
        {
            objNTD = new objDoanhNghiep();
        }
        if (!IsPostBack)
        {
            LoadTP();
            LoadQG();
            LoadSoHuu();
            LoadKCN();
            txt_ten_doanh_nghiep.Text = objNTD.ten_doanh_nghiep;
        }
        
        
    }
    protected void LoadTP() {
        Common comm = new Common();

        string sqlComm = "Select ma_dia_chi, ten_dia_chi from DanhMuc_dia_chi where muc=0 order by uu_tien,ten_dia_chi ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_tinhthanh.Items.Add(new ListItem(dtt.Rows[i]["ten_dia_chi"].ToString(),dtt.Rows[i]["ma_dia_chi"].ToString()));
        }
    }
    protected void LoadQG()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_quoc_gia, ten_quoc_gia from DanhMuc_quoc_gia  order by uu_tien,ten_quoc_gia";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_quoc_gia.Items.Add(new ListItem(dtt.Rows[i]["ten_quoc_gia"].ToString(), dtt.Rows[i]["ma_quoc_gia"].ToString()));
        }
    }
    protected void LoadSoHuu()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_so_huu, ten_so_huu from DanhMuc_so_huu   ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_kieu_so_huu.Items.Add(new ListItem(dtt.Rows[i]["ten_so_huu"].ToString(), dtt.Rows[i]["ma_so_huu"].ToString()));
        }
    }
    protected void LoadKCN()
    {
        Common comm = new Common();

        string sqlComm = "Select ma_khu_cn, ten_khu_cn from DanhMuc_khu_cong_nghiep   ";
        DataTable dtt = comm.getDataTable(sqlComm);
        for (int i = 0; i < dtt.Rows.Count; i++)
        {
            cbb_khu_cn.Items.Add(new ListItem(dtt.Rows[i]["ten_khu_cn"].ToString(), dtt.Rows[i]["ma_khu_cn"].ToString()));
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["NTDobj"] != null)
        {
            objNTD = (objDoanhNghiep)Session["NTDobj"];
        }
        else
        {
            objNTD = new objDoanhNghiep();
        }
            string thumb = SaveFile(txt_hinh_anh, "~/Uploads/Images/",objNTD.tai_khoan);
            if (thumb.Length == 0) thumb = "";
            objNTD.chuc_vu = txt_chuc_vu.Text.Trim();
            objNTD.dia_chi_chitiet = txt_dia_chi_chi_tiet.Text.Trim();
            objNTD.dien_thoai = txt_dien_thoai.Text.Trim();
            objNTD.fax = txt_fax.Text.Trim();
            objNTD.hinhanh = thumb;
            objNTD.ma_dia_chi = txt_dia_chi_THX_hidden.Value.Trim();
            objNTD.ma_khu_cn = cbb_khu_cn.SelectedValue;
            objNTD.ma_quoc_gia = cbb_quoc_gia.SelectedValue;
            objNTD.ma_so_huu = cbb_kieu_so_huu.SelectedValue;
            objNTD.ma_so_thue = txt_ma_so_thue.Text.Trim();
			if(txt_nam_thanh_lap.Text.Trim()=="") objNTD.nam_thanh_lap="0";
			else
				objNTD.nam_thanh_lap = txt_nam_thanh_lap.Text.Trim();
            objNTD.nguoi_lien_he = txt_nguoi_dai_dien.Text.Trim();
            objNTD.yahoo = txt_yahoo.Text.Trim();
            objNTD.skype = txt_skype.Text.Trim();
            try
            {
                objNTD.so_lao_dong = Convert.ToInt32(txt_so_lao_dong.Text.Trim());
            }
            catch (Exception)
            {

                objNTD.so_lao_dong = 0;
            }
           
            objNTD.ten_tat = txt_tenviettat.Text.Trim();
            objNTD.website = txt_website.Text;
            
            objNTD.nganh_kd = new ListItemCollection();
            
            char[] splChar = new char[] { '#' };
            
            for (int i = 0; i < list_nganh_kd_hidden.Value.Split(splChar).Length-2; i+=2)
            {
               
                objNTD.nganh_kd.Add(new ListItem(list_nganh_kd_hidden.Value.Split(splChar)[i+1], list_nganh_kd_hidden.Value.Split(splChar)[i]));
            }
            

            Session.Add("NTDobj", objNTD);
            Response.Redirect("~/web/pages/dangkynhatuyendung_final.aspx");
        
    }
    public string SaveFile(FileUpload file, string folder,string name)
    {
        try
        {
            if ((file.PostedFile != null) && (file.PostedFile.InputStream.Length > 0))
            {
                char[] slp = new char[] { '.' };
                string fileEx = file.PostedFile.FileName.Split(slp)[file.PostedFile.FileName.Split(slp).Length-1].ToString();
                if (fileEx.ToLower().Equals("jpg") || fileEx.ToLower().Equals("png") || fileEx.ToLower().Equals("gif") || fileEx.ToLower().Equals("jpge"))
                {

                    string fileName = "NTD_" + name + "_logo." + fileEx;
                    string saveName = Server.MapPath(folder) + fileName;
                    file.SaveAs(saveName);
                    return fileName;
                }
                else
                    return "";
            }else
                return "";
        }
        catch (Exception)
        {

            return "";
        }
    }
}
